#!/bin/bash

toolchain_url="https://occ-oss-prod.oss-cn-hangzhou.aliyuncs.com/resource//1732862099878/Xuantie-900-gcc-elf-newlib-x86_64-V3.0.1-20241120.tar.gz"
toolchain_dir=$(pwd)/toolchain

if [ ! -d $toolchain_dir ]; then
    mkdir -p $toolchain_dir
fi

# Download toolchain
if [ -f $toolchain_dir/Xuantie-900-gcc-elf-newlib-x86_64-V3.0.1-20241120.tar.gz ]; then
    echo "Xuantie-900-gcc-elf-newlib-x86_64-V3.0.1-20241120.tar.gz already exists"
else
    wget -c $toolchain_url -P $toolchain_dir
    echo "Download Xuantie-900-gcc-elf-newlib-x86_64-V3.0.1-20241120.tar.gz done"
fi

# Extract toolchain
if [ -d $toolchain_dir/Xuantie-900-gcc-elf-newlib-x86_64-V3.0.1 ]; then
    echo "Xuantie-900-gcc-elf-newlib-x86_64-V3.0.1-20241120.tar.gz already extracted"
else
    tar -zxvf $toolchain_dir/Xuantie-900-gcc-elf-newlib-x86_64-V3.0.1-20241120.tar.gz -C $toolchain_dir
    echo "Extract toolchain done"
fi

export TOOL_EXTENSION=$toolchain_dir/Xuantie-900-gcc-elf-newlib-x86_64-V3.0.1/bin
